// eslint-disable-next-line strict
const crypto = require('crypto');
const si = require('systeminformation');

async function getDiskSerialNum() {
  const res = await si.diskLayout();
  const result = [];
  for (let i = 0; i < res.length; i++) {
    result.push(res[i].serialNum);
  }
  return result.join('');
}
async function getMemSerialNum() {
  const res = await si.memLayout();
  const result = [];
  for (let i = 0; i < res.length; i++) {
    result.push(res[i].serialNum);
  }
  return result.join('');
}

// 生成唯一License
function generateLicense() {
  const mem = getMemSerialNum();
  const disk = getDiskSerialNum();
  return Promise.all([mem, disk]).then(([memSn, diskSn]) => {
    const data = `${memSn}xyvision${diskSn}`;
    const hash = crypto.createHash('md5').update(data).digest('hex');
    return hash;
  });
}

module.exports = { generateLicense };
